import { reqGoodsInfo, reqAddOrShopCar } from '@/api'

const state = {
    goodsInfo: {},
  
}
const mutations = {
    GETREQGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}
const actions = {
    // detail详情页请求
    async getReqGoodsInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code === 200) {
            commit('GETREQGOODSINFO', result.data)
        }
    },
    // 添加至购物车请求 || 更新购物车产品
    // 通过状态码判断后 给出相应的promise状态,派发请求的组件就可通过async返回的promise状态对此次请求任务的成功与否做出相应的逻辑
    async getReqAddOrShopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrShopCar(skuId, skuNum)
        if (result.code === 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('添加购物车失败'))
        }
    }

}
const getters = {
    // state中goodsInfo初始为空对象,空对象不会有categoryView属性 所以会出现假报错（当请求回来后goodsInfo就有数据了）
    categoryView(state) {
        //当前计算出的  categoryView属性的属性值至少是一个空对象,就不会假报错了
        return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || []
    }

}
export default {
    state,
    mutations,
    actions,
    getters,
}